package com.example.blackbox.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.blackbox.Data.Game;
import com.example.blackbox.R;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class GameAdapter extends BaseAdapter {
    ArrayList<Game> games=new ArrayList<>();
    Context context;

    public GameAdapter(ArrayList<Game> games, Context context) {
        this.games = games;
        this.context = context;
    }

    @Override
    public int getCount() {
        return games.size();
    }

    @Override
    public Object getItem(int position) {
        return games.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView==null){
            viewHolder=new ViewHolder();
            convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_game,null);
            viewHolder.IVImageView=convertView.findViewById(R.id.game_img);
            viewHolder.TvGameName=convertView.findViewById(R.id.game_name);
            viewHolder.TVGameTime=convertView.findViewById(R.id.game_time);

            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Game game=games.get(position);
        Glide.with(context).load(game.getGameImgUrl()).into(viewHolder.IVImageView);
        viewHolder.TvGameName.setText(game.getGameName());
        viewHolder.TVGameTime.setText(game.getGameTime());

        return convertView;
    }

    class ViewHolder{
        ImageView IVImageView;
        TextView TvGameName;
        TextView TVGameTime;
    }
}
